#include ".\dxMgr.h"
#include ".\dxUtility.h"
#include ".\dxMeshAnimation.h"
#include ".\dxCustomVertex.h"
//#include ".\dxCollada.h"
#include ".\dxCamera.h"
#include ".\Common\dxTypes.h"
#include ".\Common\dxDirectives.h"
#include ".\dxInput.h"
#include ".\dxLight.h"
#include ".\dxAnimation.h"
#include ".\dxMenu.h"
#include ".\dxSound.h"
#include ".\dxGameMgr.h"
#include <stdlib.h>

static bool wireMode;
static bool changeFrame;
static float i;
static float posTemp;
static float decelSpeed;

dxMgr::dxMgr(void)
{
	direct3d = 0;
	d3dd3v = 0;
	modelAnimation = 0;
	modelAnimation2 = 0;
	modelAnimation3 = 0;
	modelAnimation4 = 0;
	
	displayControls = true;
	modelPosition = D3DXVECTOR3(-105,-5, 120);//terrain
	modelPosition2 = D3DXVECTOR3(10,-3,50);//car
	modelPosition3 = D3DXVECTOR3(10,-3,50);//car

	//modelPosition4 = D3DXVECTOR3(-20,-4,60);//alien
	m_entityYaw = 0;
	m_entityPitch = 0;
	m_entityRoll = 0;
	m_entityYaw2 = 0;
	m_entityPitch2 = 0;
	m_entityRoll2 = 0;
	m_entityYaw3 = 0;
	m_entityPitch3 = 0;
	m_entityRoll3 = 0;
	m_font = 0;
	m_camera=new dxCamera();
	m_camera2=new dxCamera();
	m_camera3=new dxCamera();
	m_camera4=new dxCamera();
	windowed = NULL;
	changeFrame = false;
	boolMenu = false;
	boolMessage = false;
	Loader = false;
	
	turnCar3 = false;
}

dxMgr::~dxMgr(void)
{
	if (modelAnimation)
	{
		delete modelAnimation;
		modelAnimation=0;
	}
	if (modelAnimation2)
	{
		delete modelAnimation2;
		modelAnimation2 = 0;
	}
	if (modelAnimation3)
	{
		delete modelAnimation3;
		modelAnimation3 = 0;
	}
	if (modelAnimation4)
	{
		delete modelAnimation3;
		modelAnimation3 = 0;
	}

	if (m_camera)
	{
		delete m_camera;
		m_camera=0;
	}
	if (m_camera2)
	{
		delete m_camera2;
		m_camera2=0;
	}
	
	if (m_camera3)
	{
		delete m_camera3;
		m_camera3=0;
	}
	
	if (m_camera4)
	{
		delete m_camera4;
		m_camera4=0;
	}

	if (m_font)
	{
		m_font->Release();
		m_font=0;
	}

	if (d3dd3v)
	{
		d3dd3v->Release();
		d3dd3v=0;
	}

	if (direct3d)
	{
		direct3d->Release();
		direct3d=0;
	}
}

bool dxMgr::Initialise(HWND hWnd, HINSTANCE hInst, int width, int height, bool fullscreen)
{	
	direct3d=Direct3DCreate9(D3D_SDK_VERSION);
	if (!direct3d)
	{
		CUtility::DebugString("Could not create Direct3D object\n");
		return false;
	}
	
	if (fullscreen)
	{
        windowed = true;
    }

	setupD3DPRESENT(hWnd, width, height, fullscreen);

	HRESULT hr=direct3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,hWnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING, 
		&d3dpp, 
		&d3dd3v);
			
	if (CUtility::FailedHr(hr))
	{
		CUtility::DebugString("Could not create Direct3D device\n");
		return false;
	}
	
	HRESULT hrSprite = D3DXCreateSprite(d3dd3v, &d3dspt);
	if (CUtility::FailedHr(hrSprite))
	{
		CUtility::DebugString("Could not create D3DXCreateSprite\n");
		return false;
	}
	
	std::string texturePath = CUtility::GetTheCurrentDirectory()+"/data/DisplaySprites.png";
	LoadTexture(&DisplayTexture, texturePath.c_str()); 

	//InitialiseCollada();
	modelRenderState(d3dd3v);
	//customVertexRenderState(d3dd3v);

	return true;
}

bool dxMgr::setupD3DPRESENT(HWND hWnd, int width, int height, bool fullscreen)
{
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    //memset(&d3dpp,0,sizeof(d3dpp));
    d3dpp.Windowed = windowed; //Windowed or Fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //discards the previous frames
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; //The display format
	d3dpp.BackBufferCount  = 1;	//Number of back buffers
	d3dpp.BackBufferHeight = height; //height of the backbuffer
	d3dpp.BackBufferWidth  = width; //width of the backbuffer
	d3dpp.hDeviceWindow    = hWnd; //handle to our window
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16; //The stencil format
    d3dpp.EnableAutoDepthStencil = TRUE; //z-buffering
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;//D3DPRESENT_INTERVAL_IMMEDIATE

	return true;
}

bool dxMgr::modelRenderState(LPDIRECT3DDEVICE9 d3dd3v)
{
	d3dd3v->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
	d3dd3v->SetRenderState( D3DRS_LIGHTING, TRUE );
	d3dd3v->SetRenderState( D3DRS_DITHERENABLE, TRUE );
	d3dd3v->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
	d3dd3v->SetRenderState( D3DRS_ZENABLE, TRUE );
	d3dd3v->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
	d3dd3v->SetRenderState( D3DRS_AMBIENT, 0x99999999 );
	d3dd3v->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE );
	//d3dd3v->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
	d3dd3v->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
	d3dd3v->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	d3dd3v->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_CURRENT );
	d3dd3v->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
	d3dd3v->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
	d3dd3v->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
	d3dd3v->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC/*D3DTEXF_LINEAR*/ );
	d3dd3v->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC/*D3DTEXF_LINEAR*/ );
	d3dd3v->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(80,80,80));
	
	//d3dd3v->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);    // turn on the color blending
    //d3dd3v->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_INVDESTCOLOR);    // set source factor
    //d3dd3v->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);    // set dest factor
    //d3dd3v->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);    // set the operation

	D3DXMATRIX matProj;	
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1024.0f/768.0f, 1.0f, 5000.0f );
	d3dd3v->SetTransform( D3DTS_PROJECTION, &matProj );

	objLight = new dxLight(d3dd3v);
	objLight->initLightCustom(d3dd3v);

	D3DXCreateFont( d3dd3v, FontSize, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &m_font );
		
	//bones_all//alien//spaceship//car//terrain//testX/hose
	LoadXFile(CUtility::GetTheCurrentDirectory()+"/data/terrain_04.x",0);
	LoadXFile2(CUtility::GetTheCurrentDirectory()+"/data/car.x",0);  
	LoadXFile3(CUtility::GetTheCurrentDirectory()+"/data/car.x",0);
	//LoadXFile4(CUtility::GetTheCurrentDirectory()+"/data/test.x",0);	

	return true;
}

bool dxMgr::customVertexRenderState(LPDIRECT3DDEVICE9 d3dd3v)
{
	d3dd3v->SetRenderState(D3DRS_LIGHTING, TRUE);
	d3dd3v->SetRenderState(D3DRS_ZENABLE, TRUE);
	d3dd3v->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
	
	return true;
}

bool dxMgr::InitialiseCollada()
{
	/*objCollada = new dxCollada();
	objCollada->initLoader();*/
	
	return true;
}

dxCamera *dxMgr::GetCamera() const
{
	assert(m_camera);
	return m_camera;
}

dxCamera *dxMgr::GetCamera2() const
{
	assert(m_camera2);
	return m_camera2;
}

dxCamera *dxMgr::GetCamera3() const
{
	assert(m_camera3);
	return m_camera3;
}

dxCamera *dxMgr::GetCamera4() const
{
	assert(m_camera4);
	return m_camera4;
}

void dxMgr::beginRender(void)
{
	D3DXMATRIX matView;
	m_camera->CalculateViewMatrix(&matView);
	d3dd3v->SetTransform( D3DTS_VIEW, &matView );
	//d3dd3v->SetTransform( D3DTS_WORLD, &matView );
	
	HRESULT hr=d3dd3v->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(50,50,50), 1.0f, 0);
	
 	if (CUtility::FailedHr(hr))
		return;
	
	if(SUCCEEDED(d3dd3v->BeginScene()))
	{
		if(!Loader)
		{
			d3dspt->Begin(D3DXSPRITE_ALPHABLEND);	
			drawLoader();
			d3dspt->End();
		}
		
		if(!boolMenu && Loader)
		{
			displayModel();
			DisplayText(modelPosition.y);
		}
	}
}

void dxMgr::endRender(void)
{
	d3dd3v->EndScene();
	d3dd3v->Present( NULL, NULL, NULL, NULL ); 
}

void dxMgr::displayModel(void)
{
	static DWORD lastTime=timeGetTime();
	float timeElapsed=(float)(timeGetTime()-lastTime);
	lastTime=timeGetTime();
	
	if (modelAnimation)
	{
		D3DXMATRIX matRotX,matRotY,matRotZ,matTrans;
		D3DXMatrixRotationX( &matRotX, m_entityPitch );//y
		D3DXMatrixRotationY( &matRotY, m_entityYaw );//x  
		D3DXMatrixRotationZ( &matRotZ, m_entityRoll );//z 
		D3DXMatrixTranslation(&matTrans,modelPosition.x,modelPosition.y,modelPosition.z);
		D3DXMATRIX matWorld=(matRotX*matRotY*matRotZ)*matTrans;
		
		modelAnimation->FrameMove(timeElapsed,&matWorld);
		modelAnimation->Render();
	}
	
	if (modelAnimation2)
	{
		D3DXMATRIX matRotX2,matRotY2,matRotZ2,matTrans2;
		D3DXMatrixRotationX( &matRotX2, m_entityPitch2 );
		D3DXMatrixRotationY( &matRotY2, m_entityYaw2 );  
		D3DXMatrixRotationZ( &matRotZ2, m_entityRoll2 ); 
		D3DXMatrixTranslation(&matTrans2,modelPosition2.x,modelPosition2.y,modelPosition2.z);
		D3DXMATRIX matWorld2=(matRotX2*matRotY2*matRotZ2)*matTrans2;			
		
		PositionModel1(modelPosition2.x, modelPosition2.y, modelPosition2.z);
		
		modelAnimation2->FrameMove(timeElapsed,&matWorld2);
		modelAnimation2->Render();
	}
	
	if (modelAnimation3)
	{
		D3DXMATRIX matRotX3,matRotY3,matRotZ3,matTrans3;
		D3DXMatrixRotationX( &matRotX3, m_entityPitch3 );
		D3DXMatrixRotationY( &matRotY3, m_entityYaw3 );  
		D3DXMatrixRotationZ( &matRotZ3, m_entityRoll3 ); 
		D3DXMatrixTranslation(&matTrans3,modelPosition3.x,modelPosition3.y,modelPosition3.z);
		D3DXMATRIX matWorld3=(matRotX3*matRotY3*matRotZ3)*matTrans3;

		PositionModel2(modelPosition3.x, modelPosition3.y, modelPosition3.z);

		modelAnimation3->FrameMove(timeElapsed,&matWorld3);
		modelAnimation3->Render();
		
		modelPosition3.z = modelPosition3.z + 0.3f;
		
		int randNr = random_number_int(1, 50);
		if(randNr > 25)
		{
			static float accelerate = 0.3f;
			modelPosition3.z = modelPosition3.z + accelerate;
			accelerate+=0.002f;
			//PositionModel1("Greater: " + ToString(randNr));
		}
		else
		{
			//static float accelerate = 0.20f;
			//modelPosition3.z = modelPosition3.z - accelerate;
			//accelerate+=0.002f;
			//PositionModel1("Less: " + ToString(randNr));
		}
	}
	
	if (modelAnimation4)
	{
		float timeElapsed=(float)(timeGetTime()-lastTime);
		lastTime=timeGetTime();

		D3DXMATRIX matRotX4,matRotY4,matRotZ4,matTrans4;
		D3DXMatrixRotationX( &matRotX4, m_entityPitch4 );
		D3DXMatrixRotationY( &matRotY4, m_entityYaw4 );  
		D3DXMatrixRotationZ( &matRotZ4, m_entityRoll4 ); 
		D3DXMatrixTranslation(&matTrans4,modelPosition4.x,modelPosition4.y,modelPosition4.z);
		D3DXMATRIX matWorld4=(matRotX4*matRotY4*matRotZ4)*matTrans4;

		modelAnimation4->FrameMove(timeElapsed,&matWorld4);
		modelAnimation4->Render();
	}
}

void dxMgr::PositionModel1(float x, float y, float z) const
{
	D3DCOLOR fontColor = D3DCOLOR_XRGB(0,255,255);    
	RECT rct;
	rct.left=FontSize;
	rct.right=800;
	rct.top=FontSize;
	rct.bottom=rct.top + FontSize;
	
	rct.top += 30;rct.bottom=rct.top + 30;
		std::string positionString="Car 1 X:" + ToString(x) + "   Y:" + ToString(y) + "  Z:" + ToString(z);
	m_font->DrawText(NULL,positionString.c_str(), -1, &rct, 0, fontColor );
	
}

void dxMgr::PositionModel2(float x, float y, float z) const
{
	D3DCOLOR fontColor = D3DCOLOR_XRGB(0,255,255);    
	RECT rct;
	rct.left=FontSize;
	rct.right=800;
	rct.top=FontSize;
	rct.bottom=rct.top + FontSize;
	
	rct.top += 60;rct.bottom=rct.top + 60;
		std::string positionString="Model2 X:" + ToString(x) + "   Y:" + ToString(y) + "  Z:" + ToString(z);
	m_font->DrawText(NULL,positionString.c_str(), -1, &rct, 0, fontColor );
	
}

void dxMgr::DisplayText(float x) const
{
	static DWORD lastTime=timeGetTime();
	static int numFrames=0;
	static float fps=0;

	numFrames++;
	DWORD timePassed=timeGetTime()-lastTime;
	if (timePassed>1000)
	{
		fps=0.001f*timePassed*numFrames;
		numFrames=0;
		lastTime=timeGetTime();
	}
	
	D3DCOLOR fontColor = D3DCOLOR_XRGB(0,255,255);    
	RECT rct;
	rct.left=FontSize;
	rct.right=800;
	rct.top=FontSize;
	rct.bottom=rct.top + FontSize;

	std::string fpsString="FPS: "+ToString(fps);
	m_font->DrawText(NULL, fpsString.c_str(), -1, &rct, 0, fontColor );
	rct.top += FontSize;rct.bottom=rct.top + FontSize;

	if (modelAnimation)
	{
		//std::string fnameString="Current File: " + modelAnimation->GetFilename();
		//m_font->DrawText(NULL,fnameString.c_str(), -1, &rct, 0, fontColor );
		//
		//rct.top += FontSize;rct.bottom=rct.top + FontSize;		
		//std::string animString="Current Animation Set: ("+ToString(modelAnimation->GetCurrentAnimationSet())+") "+
		//	modelAnimation->GetAnimationSetName(modelAnimation->GetCurrentAnimationSet());
		//m_font->DrawText(NULL,animString.c_str(), -1, &rct, 0, fontColor );
	}
	else
	{
		m_font->DrawText(NULL,"No file loaded. Press O to load a file", -1, &rct, 0, fontColor );
	}
}

bool dxMgr::LoadXFile(const std::string &filename,int startAnimation)
{
	if (modelAnimation)
	{
		delete modelAnimation;
		modelAnimation=0;
	}

	modelAnimation=new dxMeshAnimation(d3dd3v);
	if (!modelAnimation->Load(filename))
	{
		delete modelAnimation;
		modelAnimation=0;
		return false;
	}

	modelAnimation->SetAnimationSet(startAnimation);

	D3DXVECTOR3 pos = modelAnimation->GetInitialCameraPosition();
	//pos = D3DXVECTOR3(5.0f,20.0f,20.0f);
	//m_camera->SetPosition(pos);
	//m_camera->SetYawPitchRoll(0.0,0,0);
	modelPosition.y = -10.0f;
	modelPosition.z = 150.0f;
	GetCamera()->MoveRight(-125.0f);
	YawEntity(1.5f);//rotation yaw - x
	GetCamera()->MoveForward(-25.5f);//in spate
	return true;
}

bool dxMgr::LoadXFile2(const std::string &filename,int startAnimation)
{
	if (modelAnimation2)
	{
		delete modelAnimation2;
		modelAnimation2=0;
	}

	modelAnimation2=new dxMeshAnimation(d3dd3v);
	if (!modelAnimation2->Load(filename))
	{
		delete modelAnimation2;
		modelAnimation2=0;
		return false;
	}

	modelAnimation2->SetAnimationSet(startAnimation);

	D3DXVECTOR3 pos = modelAnimation2->GetInitialCameraPosition();
	pos = D3DXVECTOR3(5.0f,20.0f,20.0f);
	m_camera2->SetPosition(pos);
	m_camera2->SetYawPitchRoll(0,0,0);
	YawEntity2(3.20f);
	modelPosition2.x = -140.0f;
	modelPosition2.y = -8.4f;
	modelPosition2.z = 5.0f;
	RollEntity2(-0.20f);
	PitchEntity2(0.20f);

	return true;
}

bool dxMgr::LoadXFile3(const std::string &filename,int startAnimation)
{
	if (modelAnimation3)
	{
		delete modelAnimation3;
		modelAnimation3=0;
	}

	modelAnimation3=new dxMeshAnimation(d3dd3v);
	if (!modelAnimation3->Load(filename))
	{
		delete modelAnimation3;
		modelAnimation3=0;
		return false;
	}
	
	modelAnimation3->SetAnimationSet(startAnimation);

	D3DXVECTOR3 pos = modelAnimation3->GetInitialCameraPosition();
	pos = D3DXVECTOR3(5.0f,20.0f,20.0f);
	m_camera3->SetPosition(pos);
	m_camera3->SetYawPitchRoll(0,0,0);
	YawEntity3(-15.6f);
	modelPosition3.x = -150.0f;
	modelPosition3.y = -8.4f;
	modelPosition3.z = 8.0f;
	RollEntity3(-0.15f);
	PitchEntity3(0.20f);

	return true;
}

bool dxMgr::LoadXFile4(const std::string &filename,int startAnimation)
{
	if (modelAnimation4)
	{
		delete modelAnimation4;
		modelAnimation4=0;
	}

	modelAnimation4=new dxMeshAnimation(d3dd3v);
	if (!modelAnimation4->Load(filename))
	{
		delete modelAnimation4;
		modelAnimation4=0;
		return false;
	}
	
	modelAnimation4->SetAnimationSet(startAnimation);

	D3DXVECTOR3 pos = modelAnimation4->GetInitialCameraPosition();
	pos = D3DXVECTOR3(5.0f,20.0f,20.0f);
	m_camera4->SetPosition(pos);
	m_camera4->SetYawPitchRoll(0,0,0);

	return true;
}

void dxMgr::NextAnimation()
{
	if (modelAnimation3)
		modelAnimation3->NextAnimation();
}

void dxMgr::setAnimation(unsigned int x)
{
	/*if (modelAnimation3)
		modelAnimation3->SetAnimationSet(x);*/
	//changeFrame = true;	
	modelAnimation2->processMeshData();
}

void dxMgr::wireFrameMode()
{
	if(!wireMode)
	{
		d3dd3v->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
		wireMode = true;
	}
	else
	{
		d3dd3v->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
		wireMode = false;
	}
}

void dxMgr::AnimateFaster()
{
	/*if (modelAnimation)
		modelAnimation->AnimateFaster();*/
}

void dxMgr::AnimateSlower()
{
	/*if (modelAnimation)
		modelAnimation->AnimateSlower();*/
}

void dxMgr::MoveForwardUp(void)
{
	GetCamera()->MoveForward(kCameraMovementSpeed);
	
}

void dxMgr::MoveForwardDown(void)
{
	GetCamera()->MoveForward(-kCameraMovementSpeed);		
}

void dxMgr::MoveRight(void)
{
	GetCamera()->MoveRight(kCameraMovementSpeed);	
}

void dxMgr::MoveLeft(void)
{
	GetCamera()->MoveRight(-kCameraMovementSpeed);	
}

void dxMgr::MoveUpUp(void)
{
	GetCamera()->MoveUp(kCameraMovementSpeed);	
}

void dxMgr::MoveUpDown(void)
{
	GetCamera()->MoveUp(-kCameraMovementSpeed);	
}

void dxMgr::MoveShipXL(void)
{
	modelPosition2.x = modelPosition2.x - 0.2f;
	GetCamera()->MoveRight(-kCameraMovementSpeed);
}

void dxMgr::MoveShipXR(void)
{
	modelPosition2.x = modelPosition2.x + 0.2f;
	GetCamera()->MoveRight(kCameraMovementSpeed);
}
void dxMgr::MoveShipYU(void)
{
	modelPosition2.y = modelPosition2.y - 0.2f;
	GetCamera()->MoveUp(-kCameraMovementSpeed);	
}

void dxMgr::MoveShipYD(void)
{
	modelPosition2.y = modelPosition2.y + 0.2f;
	GetCamera()->MoveUp(kCameraMovementSpeed);	
}
void dxMgr::MoveShipZF(void)
{
	modelPosition2.z = modelPosition2.z - 0.20f;
	GetCamera()->MoveForward(-kCameraMovementSpeed);
}

void dxMgr::MoveShipZB(void)
{
	static float accelCar = 0.20f;
	static float accelKamera = 0.000f;
	modelPosition2.z = modelPosition2.z + accelCar;
	GetCamera()->MoveForward(kCameraMovementSpeed + accelKamera);
	accelCar+=0.002f;
	accelKamera+=0.002f;
	decelSpeed=accelCar;

	if (decelSpeed < 0.30f)
		speedState = 1;
	else if (decelSpeed < 0.31f)
		speedState = 2;
	else if (decelSpeed<0.37f)
		speedState = 3;

}

void dxMgr::MoveShipZB2(void)
{	
	if (decelSpeed >= 0.002f)
	{
		speedState = 1;
		modelPosition2.z = modelPosition2.z + decelSpeed;
		GetCamera()->MoveForward(decelSpeed);
		decelSpeed-=0.006f;
	}
}

LPDIRECT3DDEVICE9 dxMgr::getD3DDevice()
{ 
	return d3dd3v; 
}

float dxMgr::random_number(float low, float high)
{
    return low + ((float)((float)rand() / (float)RAND_MAX) * (float)((high) - (low)));
}

int dxMgr::random_number_int(int low, int high)
{
    return rand() % high + low;
}

void dxMgr::drawLoader()
{
	RECT rt1,rt2;
	static int index = 0;
	
	if(index != 480)
	{
		SetRect(&rt1, 1, 1, 505, 12);
		SetRect(&rt2, 1, 12, index + 25, 23);
		index+=2;   
		
		DrawTexture(DisplayTexture, rt1, 250, 656, 255);
		DrawTexture(DisplayTexture, rt2, 250, 655, 255);
    }
    else
		Loader = true;
		
    return;
}

void dxMgr::LoadTexture(LPDIRECT3DTEXTURE9* texture, LPCTSTR filename)
{
    D3DXCreateTextureFromFileEx(d3dd3v, filename, D3DX_DEFAULT, D3DX_DEFAULT,
        D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, 
        D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, texture);

    return;
}

void dxMgr::DrawTexture(LPDIRECT3DTEXTURE9 texture, RECT texcoords, float x, float y, int a)
{
    D3DXVECTOR3 center(0.0f, 0.0f, 0.0f), position(x, y, 0.0f);
    d3dspt->Draw(texture, &texcoords, &center, &position, D3DCOLOR_ARGB(a,255, 255, 255));

    return;
}
